package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import org.xutils.common.util.DensityUtil;

import a66weding.com.jiehuntong.R;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/8/21
 * @description :
 */
public class DashedLineView extends View {
    private Context mContext;
    private float mDashgap;//虚线格之间间距
    private float mDashwidth;//虚线格的宽度
    private float mDashheight;//虚线格的高度
    private int mDashColor;//虚线格的颜色
    private Paint mPaint;//画笔

    public DashedLineView(Context context) {
        this(context, null);
    }

    public DashedLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DashedLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.dashLineView);
        mDashgap = typedArray.getDimension(R.styleable.dashLineView_dash_gap, DensityUtil.dip2px(4));
        mDashwidth = typedArray.getDimension(R.styleable.dashLineView_dash_width,DensityUtil.dip2px(4));
        mDashheight = typedArray.getDimension(R.styleable.dashLineView_dash_height,DensityUtil.dip2px(1));
        mDashColor = typedArray.getColor(R.styleable.dashLineView_dash_color,mContext.getResources().getColor(R.color.color_Maindevider) );

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mDashheight);//画笔的宽度，其实就是虚线的高度
        mPaint.setColor(mDashColor);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        float length = mDashgap + mDashwidth;
        int num = (int) Math.floor(width/length);
        for (int i=0;i<num;i++){
            canvas.drawLine(i*length,0,i*length+mDashwidth,height,mPaint);
        }
    }

}
